package com.xiaxiang.xiawan.My;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.xiaxiang.xiawan.FragmentBase;
import com.xiaxiang.xiawan.R;
import com.xiaxiang.xiawan.XXMainApplication;
import com.xiaxiang.xiawan.login.XXLoginActivity;

/**
 * Created by mk on 2017/10/17.
 */

public class MyFragment extends FragmentBase {
    private View view;
    TextView textUsername;
    ImageView imageHeadPhoto;
    XXMainApplication mApp = XXMainApplication.getInstance();

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.my_fragment, container, false);
        initView();
        setListener();
        return view;
    }

    private void initView() {
        ((TextView)view.findViewById(R.id.TitleBarText)).setText("我的");
        getUserInfo();
    }

    private void getUserInfo() {
        textUsername = view.findViewById(R.id.text_username);
        String username = mApp.getUserName();
        if (username != null && username.length() > 0) {
            textUsername.setText(username);
        }else {
            textUsername.setText("未登录");
        }
        imageHeadPhoto = view.findViewById(R.id.icon);
        String iconurl = mApp.getmIconUrl();
        if (iconurl != null && iconurl.length() > 0) {
            Glide.with(getActivity()).load(iconurl)
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(imageHeadPhoto);
//            Glide.with(getActivity()).load(iconurl)
//                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
//                    .into(imageHeadPhoto).onLoadFailed(getResources().getDrawable(R.mipmap.myhead));
        }else {
            imageHeadPhoto.setImageResource(R.mipmap.myhead);
        }

    }

    private void setListener() {
        if (mApp.getIsLogin()) {
            view.findViewById(R.id.user_info).setOnClickListener(null);
        }else
            {
            view.findViewById(R.id.user_info).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent it = new Intent(getActivity(), XXLoginActivity.class);
                    startActivity(it);
                }
            });
        }

        view.findViewById(R.id.myPraise).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), MyPraiseActivity.class);
                startActivity(it);
            }
        });

        view.findViewById(R.id.localPhoto).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), LocalPhotoActivity.class);
                startActivity(it);
            }
        });

        view.findViewById(R.id.help).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), HelpActivity.class);
                startActivity(it);
            }
        });

        view.findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), SettingActivity.class);
                startActivity(it);
            }
        });
    }

    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        if(!hidden) {
//            getUserInfo();
//        }
    }
    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }
}
